
package org.upt.dcda.listeners;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.jboss.resteasy.logging.Logger;

public class SessionMapper implements HttpSessionListener
{

	private static final Logger						logger		= Logger.getLogger(SessionMapper.class);

	private static final Map<String, HttpSession>	sessionMap	= new HashMap<String, HttpSession>();

	@Override
	public void sessionCreated(HttpSessionEvent arg0)
	{
		logger.info("-> sessionCreated(HttpSesssionEvent = " + arg0 + ")");

		final HttpSession session = arg0.getSession();

		synchronized (sessionMap)
		{
			sessionMap.put(session.getId(), session);
		}

		logger.info("<- sessionCreated(HttpSesssionEvent = " + arg0 + ")");
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent arg0)
	{
		logger.info("-> sessionDestroyed(HttpSesssionEvent = " + arg0 + ")");

		final HttpSession session = arg0.getSession();

		synchronized (sessionMap)
		{
			sessionMap.remove(session.getId());
		}

		logger.info("<- sessionDestroyed(HttpSesssionEvent = " + arg0 + ")");
	}

	public static HttpSession getSession(String id)
	{
		synchronized (sessionMap)
		{
			return sessionMap.get(id);
		}
	}
}
